package com.bnuz.vtl.repo;

import com.bnuz.vtl.model.PrivateNode;
import com.bnuz.vtl.model.PublicNode;
import org.springframework.data.neo4j.annotation.Depth;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PrivateNodeRepo extends Neo4jRepository<PrivateNode, String>, BaseRepo<PrivateNode, String> {

    PrivateNode findByPrivateNodeId(String privateNodeId);

    List<PrivateNode> findAllByName(String name, @Depth int depth);

    @Query("MATCH (N:PublicNode)<-[:CHILD]-(M:PrivateNode)<-[R:CHILD*]-(p) WHERE N.publicNodeId = {publicNodeId} AND M.name = {childName} RETURN M, R, COLLECT(p);")
    List<PrivateNode> findSameNameAndSameParent(@Param("publicNodeId") String publicNodeId, @Param("childName") String childName);

    @Query("MATCH (N:PublicNode)<-[:CHILD*1]-(M:PrivateNode) WHERE N.publicNodeId = {publicNodeId} RETURN M;")
    List<PrivateNode> findPublicNodeLevel1PrivateNode(@Param("publicNodeId") String publicNodeId);

    @Query("MATCH (N:PrivateNode)<-[:CHILD*1]-(M:PrivateNode) WHERE N.publicNodeId = {privateNodeId} RETURN M;")
    List<PrivateNode> findNextLevelPrivateNode(@Param("privateNodeId") String privateNodeId);

    @Query("MATCH (N:PrivateNode) WHERE N.privateNodeId = {nodeId} DETACH DELETE N;" )
    void deleteNode(@Param("nodeId") String nodeId);

    @Query("MATCH (P:PrivateNode)-[CHILD*]->(N:PublicNode)-[r:CHILD*1]->(S:Subject) WHERE P.privateNodeId = {privateNodeId} RETURN N")
    PublicNode getParent(@Param("privateNodeId") String privateNodeId);
}
